<template>
<view>
	<!--聊天信息 -->
	
	<view class="content">
		<view class="title">群聊成员</view>
		
		<view class="user-box">
			<view class="item" v-for="(item,index) in data.userlist" :key="index">
				<image 
				:src="item.avatar"
				class="avatar"
				mode="aspectFill"
				></image>
				
				<view class="nickname text-cut">{{item.nickname || ''}}</view>
			</view>
			
			<view class="item add-box" @click="$wanlshop.to('/pages/new/chatgroup-add?group_id='+group_id)">+</view>
			<view class="item add-box" @click="$wanlshop.to('/pages/new/chatgroup-reduce?group_id='+group_id)">-</view>
		</view>
		
		<view class="flex align-center justify-center w100">
			<view class="flex align-center" @click="$wanlshop.to('/pages/new/chatgroup-view?group_id='+group_id)">
				<text class="more-title">查看{{data.user_total}}名群成员</text>
				<image src="/static/images/right.png" class="right-image"></image>
			</view>
		</view>
	</view>
	
	<view class="item-box">
		<view class="title2">群聊名称</view>
		<view class="flex align-center" @click="openMadal()">
			<text class="chat-name text-cut">{{data.name || ''}}</text>
			<image src="/static/images/right.png" class="right-image"></image>
		</view>
	</view>
	
	<view class="fixed-btn">
		<view class="btn" @click="quitGroup()">退出群聊</view>
		<view class="btn mt20" v-if="data.is_del" @click="delGroup()">删除群聊</view>
	</view>
	
	
	<uniPopup ref="uniPopup">
		<view class="warp">
			<view class="modal-body">
				<image :src="$IMG_URL + '/static/images/modal_bg.png'" class="modalImage"></image>
				<view class="modal-box">
					<view class="flex align-center justify-center pb50">
						<view class="modal-title">修改群名称</view>
						<image src="/static/images/close.png" class="close-image" @click="closeModel"></image>
					</view>
					
					<view class="input-box">
						<view class="input-title">群聊名称</view>
						<input placeholder="请输入群聊名称" 
						placeholder-style='text-align:right;font-size: 26rpx;'
						v-model="name"
						></input>
					</view>
					
					<view class="input-box">
						<view class="input-title">群聊图片</view>
						
						<view class="u-flex" @tap="onChooseImg">
							<image class="avatar" :src="fullurl" mode=""></image>
							<text class="u-iconfont uicon-arrow-right u-m-l-20" style="color:#999;"></text>
						</view>
					</view>
					
					<view class="pay-btn" @click="modifyName()">确认</view>
					
				</view>
			</view>
		</view>
	</uniPopup>
	
</view>
	
</template>

<script>
import uniPopup from '@/components/uni-popup/uni-popup';
import { BASE_URL, API_URL, IMG_URL } from '@/env.js';
//import Auth from "@/shopro/permission/index.js";
import { mapState, mapActions } from 'vuex';
export default {
	components: {
		uniPopup,
	},
	data() {
		return {
			data:{},
			group_id:0, //群聊ID
			showModal:false,
			name:'',
			EMOJI_BASE_URL: BASE_URL,
			token: uni.getStorageSync('token'),
			fullurl:'',
			uploadurl:'',
		}
	},
	onLoad(option) {
		if(option.group_id){
			this.group_id = option.group_id;
		}
		
		this.loadData();
	},
	methods: {
		async loadData() {
			uni.request({
				url: '/chat/groupshow',
				data: {
					id: this.group_id
				},
				success: res => {
					res = res.res;
					if (res.code === 1) {
						this.data = res.data;
						this.name = res.data.name;
						this.fullurl = res.data.image;
						
						let tmp = res.data.image.split("/uploads");
						this.uploadurl = "/uploads"+tmp[1];
					}
				}
			});
		
			uni.stopPullDownRefresh();
		},
		quitGroup(){
			uni.showModal({
				content: '确定要退出群聊？',
				success: res => {
					if (res.confirm) {
						uni.request({
							url: '/chat/delgroup',
							data: {
								id: this.group_id,
								type: 0,
							},
							success: res => {
								res = res.res;
								if (res.code === 1) {
									uni.reLaunch({
										url:'/pages/new/chatgroup'
									})
								}
							}
						});
						
					}
				}
			});
		},
		delGroup(){
			uni.showModal({
				content: '确定要删除群聊？',
				success: res => {
					if (res.confirm) {
						uni.request({
							url: '/chat/delgroup',
							data: {
								id: this.group_id,
								type: 1,
							},
							success: res => {
								res = res.res;
								if (res.code === 1) {
									uni.reLaunch({
										url:'/pages/new/chatgroup'
									})
								}
							}
						});
					}
				}
			});
		},
		openMadal(){
			this.$refs.uniPopup.open();
			this.showModal = true;
		},
		closeModel(){
			this.$refs.uniPopup.close();
			this.showModal = false;
		},
		modifyName(){
			if(!this.name){
				this.$wanlshop.msg('请输入群聊名称');
				return false;
			}
			
			uni.request({
				url: '/chat/groupname',
				data: {
					id: this.group_id,
					name: this.name,
					image: this.uploadurl,
				},
				success: res => {
					res = res.res;
					if (res.code === 1) {
						this.$wanlshop.msg('修改成功');
						this.showModal = false;
						this.$refs.uniPopup.close();
						this.loadData();
					}else{
						this.$wanlshop.msg(res.msg);
					}
				}
			});
		},
		// 发送图片
		uploadSuccess(list) {
			let that = this;
			list.forEach(item => {
				that.fullurl = item.response.data.fullurl;
				that.uploadurl = item.response.data.url;
				
				that.$forceUpdate();
			});
		},
		uploadChange(e) {
			// console.log('上传回调', e);
		},
		// 更换头像
		async onChooseImg() {
			// let authState = 0;
			// authState += await new Auth("writePhotosAlbum").check();
			// if (authState < 1) return;
			// authState += await new Auth("camera").check();
			// if (authState < 2) return;
			
			const chooseImageRes = await this.chooseImage(1);
			const uploadImageRes = await this.uploadImage(chooseImageRes[0]);
			this.fullurl = uploadImageRes.fullurl;
			this.uploadurl = uploadImageRes.url;
		},
		// 图片处理-选择图片
		async chooseImage(count = 1) {
			return new Promise((resolve, reject) => {
				uni.chooseImage({
					count: count, //默认9
					sizeType: ["original", "compressed"], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ["album"], //从相册选择
					success: res => {
						resolve(res.tempFilePaths);
					}
				});
			}).catch(e => {
				console.log(e);
			});
		},
		// 图片处理-上传图片
		async uploadImage(url) {
			const that = this;
			return new Promise((resolve, reject) => {
				this.$wanlshop.msg('上传中...');

				uni.request({
					url: '/wanlshop/common/uploadData',
					success: updata => {
						uni.uploadFile({
							url: updata.data.uploadurl,
							filePath: url,
							name: 'file',
							formData: updata.data.storage == 'local' ? null : updata.data.multipart,
							success: res => {
								this.$wanlshop.msg('上传成功');
								resolve(JSON.parse(res.data).data);
							},
							fail: error =>{
								console.log("error", error);
								this.$wanlshop.msg('上传失败');
							}
						});
					}
				});
				
			}).catch(e => {
				console.log(e);
			});
		}
	}
}	
</script>


<style lang="scss" scoped>
.warp {
	display: flex;
	align-items: center;
	justify-content: center;
	height: 100%;
}
.tools-item {
	align-items: center;
	justify-content: center;
	display: flex;
	flex-direction: column;
	.item-img {
		width: 70rpx;
		height: 70rpx;
	}
	
	.item-tile {
		font-size: 24rpx;
		font-weight: 500;
		color: #333333;
	}
}
		
		
.content{
	margin: 20rpx 24rpx 20rpx 24rpx;
	background-color: #ffffff;
	border-radius: 20rpx;
	padding: 28rpx;
}	

.title{
	font-size: 30rpx;
	font-family: PingFang SC;
	font-weight: 400;
	color: #292929;
}

.user-box{
	margin-top: 39rpx;
	margin-bottom: 39rpx;
	display: flex;
	justify-content: flex-start;
	flex-wrap: wrap;
}

.avatar{
	width: 64rpx;
	height: 64rpx;
	border-radius: 50%;
}

.nickname{
	font-size: 22rpx;
	font-family: PingFang SC;
	font-weight: 400;
	color: #292929;
	margin-top: 15rpx;
	width: 86rpx;
}

.item{
	display: flex;
	flex-direction: column;
	margin-right: 56rpx;
	margin-top: 35rpx;
	align-items: center;
}

.item:nth-child(5n){
	margin-right: 0rpx !important;
}

.more-title{
	font-size: 26rpx;
	font-family: PingFang SC;
	font-weight: 400;
	color: #999999;
}
.w100{
	width: 100%;
}

.item-box{
	background-color: #ffffff;
	border-radius: 20rpx;
	margin: 0rpx 24rpx 0rpx 24rpx;
	display: flex;
	align-items: center;
	justify-content: space-between;
	padding: 36rpx 28rpx;
}

.title2{
	font-size: 30rpx;
	font-family: PingFang SC;
	font-weight: 400;
	color: #292929;
}

.chat-name{
	font-size: 28rpx;
	font-family: PingFang SC;
	font-weight: 400;
	color: #999999;
}

.colorgrey{
	color: #999999;
}

.btn{
	width: 702rpx;
	height: 88rpx;
	background: #ffffff;
	border-radius: 20rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	color: #F53938;
}

.fixed-btn{
	display: flex;
	align-items: center;
	justify-content: center;
	position: fixed;
	bottom: 80rpx;
	left: 24rpx;
	flex-direction: column;
}

.add-box{
	width: 64rpx;
	height: 64rpx;
	background: #F7F7F7;
	border: 1rpx solid #EBEBEB;
	border-radius: 50%;
	color: #CCCCCC;
	display: flex;
	align-items: center;
	justify-content: center;
	font-size: 40rpx;
}

.mt20{
	margin-top: 20rpx;
}

.modal-body{
	height: 580rpx;
	background-color: #FFFFFF;
	border-radius: 20rpx;
	position: relative;
}

.modal-middle-title{
	display: flex;
	align-items: center;
	justify-content: center;
	margin-top: 12rpx;
}

.modalImage{
	width: 698rpx;
	height: 204rpx;
}

.modal-box{
	position: absolute;
	top: 30rpx;
	left: 0rpx;
	width: 100%;
	padding-left: 28rpx;
	padding-right: 28rpx;
	padding-bottom: 28rpx;
}

.modal-title{
	font-size: 32rrpx;
	font-family: PingFang SC;
	font-weight: 700;
	color: #292929;
}

.close-image{
	width: 42rpx;
	height: 42rpx;
	position: absolute;
	top: 0rpx;
	right: 28rpx;
}

.modal-money{
	font-size: 24rpx;
	font-family: PingFang SC;
	font-weight: 400;
	color: #292929;
	margin-top: 42rpx;
	margin-bottom: 27rpx;
}


.input-box{
	width: 646rpx;
	height: 88rpx;
	background: #FFFFFF;
	border: 1px solid #CCCCCC;
	border-radius: 20rpx;
	display: flex;
	justify-content: space-between;
	align-items: center;
	padding: 0rpx 32rpx;
	margin-bottom: 32rpx;
	
	input{
		text-align: right;
		width: 350rpx;
	}
}

.pay-btn{
	width: 646rpx;
	height: 88rpx;
	background: #366BEB;
	border-radius: 20rpx;
	font-size: 28rpx;
	font-family: PingFang SC;
	font-weight: 400;
	color: #FFFFFF;
	display: flex;
	align-items: center;
	justify-content: center;
	margin-top: 64rpx;
}

.pb50{
	padding-bottom: 100rpx;
}

.right-image{
	width: 30rpx;
	height: 30rpx;
}

.uni-popup{
	background-color: rgba(0,0,0,0.5);
}
</style>